package com.singlemountain.gcommerce.web.account;

import com.singlemountain.gcommerce.SysParameters;
import com.singlemountain.gcommerce.domain.user.Userd;
import com.singlemountain.gcommerce.web.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;
import java.net.Authenticator;

/**
 * Created by IntelliJ IDEA.
 * User: sunny
 * Date: 12-2-17
 * Time: 下午4:13
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class LoginController extends BaseController {

    //初始化userd的对象
    @ModelAttribute
    public Userd setModel(){
        return new Userd();
    }
    //点击登录按钮后显示登录页面
    @RequestMapping(value = URL_LOGIN,method = RequestMethod.GET)
    public String SetUp(){
        return VIEW_LOGIN;
    }
    
    
    //验证登录用户
    @RequestMapping(value = URL_LOGIN,method = RequestMethod.POST)
    public String Login(Userd userd,HttpSession userSession){
          Userd user= userdService.findUserdByEmail(userd.getEmail());
          if(user!=null&&user.getPassword().equals(userd.getPassword())){
                userSession.setAttribute(SysParameters.SESSION_USER,user);
          } else {
              return VIEW_LOGIN;
          }
      return  VIEW_MY_ACCOUNT;
    }


}
